package com.zhang.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.gulimall.common.utils.PageUtils;
import com.zhang.gulimall.common.utils.Query;

import com.zhang.gulimall.ware.dao.WareInfoDao;
import com.zhang.gulimall.ware.entity.WareInfoEntity;
import com.zhang.gulimall.ware.service.WareInfoService;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                new QueryWrapper<WareInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByParams(Map<String, Object> params) {
        IPage<WareInfoEntity> page = new Page<>();
        QueryWrapper<WareInfoEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        try {
            if (!key.isEmpty()){
                wrapper.lambda().eq(WareInfoEntity::getId,key)
                        .or().like(WareInfoEntity::getName,key)
                        .or().like(WareInfoEntity::getAddress,key)
                        .or().eq(WareInfoEntity::getAreacode,key);
            }
        }catch (NullPointerException ignored){

        } finally {
            page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                wrapper
            );
        }

        return new PageUtils(page);
    }

}